package model

import (
	"database/sql"
	"github.com/gogf/gf/container/glist"
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/frame/g"
	"github.com/silenceper/wechat/v2"
	"github.com/silenceper/wechat/v2/cache"
	"github.com/silenceper/wechat/v2/officialaccount"
	offConfig "github.com/silenceper/wechat/v2/officialaccount/config"
	"time"
)

var GlobalOfficialAccount *officialaccount.OfficialAccount
var ProcessDeviceList *glist.List
var DFXW_TOKEN string
var DFXW_TOKEN_TYPE string

var DFXW_FEED_STANDARD_DATA gdb.List
var TaosDB *sql.DB

var Redis_Node_Prefix string
var SysName string

type ProcessDevice struct {
	Addr   string
	TryNum int
}

type Record struct {
	Readtime       time.Time `json:"-"`
	Rid            uint32    `json:"rid"`
	Hnum           string    `json:"hnum"`
	Rectime        time.Time `json:"-`
	Mode           int       `json:"mode"`
	Flag_mode      int       `json:"flag_mode"`
	Flag_mode_bits string    `json:"flag_mode_bits"`
	Flag1          int       `json:"flag1"`
	Cnt_gap_time   int       `json:"cnt_gap_time"`
	Emergency      int       `json:"emergency"`
	Event          string    `json:"event"`
	Feed_type      int       `json:"feed_type"`
	Begin_end      int       `json:"begin_end"`
	Err            int       `json:"err"`
	Auto           int       `json:"auto"`
	Food_left      int       `json:"food_left"`
	Remote         int       `json:"remote"`
	Tb             int       `json:"tb"`
	Tb_record      int       `json:"tb_record"`
	Max_feed       float32   `json:"max_feed"`
	Amount         float32   `json:"amount"`
	Day_use_feed   float32   `json:"day_use_feed"`
	Day_use_water  float32   `json:"day_use_water"`
	V22            float32   `json:"v22"`
	I41            float32   `json:"i41"`
	Vin            float32   `json:"vin"`
	Vin_calc       float32   `json:"vin_calc"`
	Motor_vol      float32   `json:"motor_vol"`
	Motor_cur      float32   `json:"motor_cur"`
	Step_mr        int       `json:"step_mr"`
	Debug          int       `json:"debug"`
	Addr           string    `json:"addr"`
	Sn             string    `json:"sn"`

	//已下用于解析
	ReadtimeFormat string `json:"readtime"`
	RectimeFormat  string `json:"rectime"`
}

func init() {

	wc := wechat.NewWechat()
	//这里本地内存保存access_token，也可选择redis，memcache或者自定cache
	memory := cache.NewMemory()
	cfg := &offConfig.Config{
		AppID:     g.Config().GetString("wechat.AppID"),
		AppSecret: g.Config().GetString("wechat.AppSecret"),
		Token:     g.Config().GetString("wechat.Token"),
		//EncodingAESKey: "xxxx",
		Cache: memory,
	}
	GlobalOfficialAccount = wc.GetOfficialAccount(cfg)
	ProcessDeviceList = glist.New(true)

	DFXW_TOKEN = ""
	DFXW_TOKEN_TYPE = ""
	g.Log().Info("global init ok!")
}
